<template>
  <div class="school">
    <h2>学校名称：{{ name }}</h2>
    <h2>学校地址：{{ address }}</h2>
  </div>
</template>

<script>
import pubsub from "pubsub-js";
export default {
  name: "SchoolInfo",
  data() {
    return {
      name: "石大",
      address: "青岛",
    };
  },
  mounted() {
    // console.log("School", this);
    // this.$bus.$on("hello", (data) => {
    //   console.log("我是School，已收到数据", data);
    // });

    // 订阅消息
    this.pubId = pubsub.subscribe("hello", (msgName, data) => {
      console.log("有人发布了hello消息，回调函数被执行");
      console.log(msgName);
      console.log(data);

      // 若用function：this 是undefined
      // 若用箭头函数：this 是school组件vc
      console.log(this);
    });
  },
  beforeDestroy() {
    // 若销毁组件，应该取消绑定的全局自定义事件
    // this.$bus.$off("hello");

    // 通过订阅id取消订阅
    pubsub.unsubscribe(this.pubId);
  },
};
</script>
<style scoped lang="less">
.school {
  background-color: skyblue;
  padding: 5px;
}
</style>